#!/usr/bin/python

import Queue
import threading
from CardParser import CardParser
from CardRetriever import CardRetriever
from FileAppender import FileAppender

class CardLoader():

    def __init__(self, appender=None,thread_number=5, queue_size=50, simulate=False, update=False):
        self._tnum = thread_number if thread_number>0 else 5
        self._qsize = queue_size if queue_size>0 else 50
        self.endFlag = False
        self._simulate = simulate
        self._queue = Queue.Queue(self._qsize)
        self._queueLock = threading.Lock()
        self._appender = appender
        self._update = update

    def load(self):
        parser = []
        retriever = CardRetriever(self._appender,self._queue,self._queueLock,self._update)
        retriever.start()

        for i in range(1,self._tnum+1):
            p = CardParser(self._appender,self._queue,self._queueLock,str(i),self._simulate,self._simulate)
            p.start()
            parser.append(p)

        retriever.join()

        for t in parser:
            t.join()






'''
log = FileAppender("loader.log")
loader = CardLoader(log,10,50,False,False)
loader.load()

#The synchronized queue
queue = Queue.Queue(50)
queueLock = threading.Lock()
endFlag = False

parser = []
appender = None
retriever = CardRetriever(appender,queue,queueLock)

retriever.start()

for i in range(1,5):
    p = CardParser(appender,queue,queueLock,str(i))
    p.start()
    parser.append(p)



for t in parser:
    t.join()

retriever.join()
'''
#print 'End Card Loader'

